exports.__esModule = true;

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const assert = require('assert');
const onFinished = require('on-finished');
const destroy = require('destroy');
exports.default = class extends think.BaseController {
    //获取通知列表
    getNotifyListAction() {
        var _this = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const data = _this.post('page,pageSize,user_id');

            if (!data) {
                aj.success = false;
                aj.msg = '请传入查询参数';
            } else {
                const notifyService = _this.service('api/office/OaService');
                const result = yield notifyService.notifyList(data);

                const typeResult = yield notifyService.getNotifyType();

                // aj.result = result

                if (!result.error) {
                    aj.result = result;
                } else {
                    aj.success = false;
                    aj.msg = ' 查询失败';
                }
            }
            return _this.json(aj);
        })();
    }

    //获取通知详情
    getNotifyDetailsAction() {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const data = _this2.post();
            if (!data) {
                aj.success = false;
                aj.msg = '请传入查询参数';
            } else {
                const detailService = _this2.service('api/office/OaService');
                const res = yield detailService.notifyDetails(data);
                if (res.result.length > 0) {
                    let fileArr = [];
                    for (let item of res.result) {
                        // let num = item.realpath.lastIndexOf('/');
                        _this2.serverIP = 'http://' + _this2.ctx.hostname + ':' + think.config('proxyPort');
                        let file = {
                            file_path: item.realpath,
                            file_name: item.file_name
                        };
                        fileArr.push(file);
                    }
                    let notifyInfo = {
                        id: res.res.id,
                        type: res.res.type,
                        title: res.res.title,
                        content: res.res.content,
                        files: fileArr,
                        status: res.res.status,
                        create_by: res.res.create_by,
                        create_date: res.res.create_date,
                        update_by: res.res.update_by,
                        update_date: res.res.update_date,
                        remarks: res.res.remarks,
                        del_flag: res.res.del_flag
                        // filename: res.res.filename
                    };

                    if (!res.error) {
                        aj.result = notifyInfo;
                    } else {
                        aj.success = false;
                        aj.msg = '获取失败';
                    }
                } else {
                    let notifyInfo = {
                        id: res.res.id,
                        type: res.res.type,
                        title: res.res.title,
                        content: res.res.content,
                        files: res.res.files,
                        status: res.res.status,
                        create_by: res.res.create_by,
                        create_date: res.res.create_date,
                        update_by: res.res.update_by,
                        update_date: res.res.update_date,
                        remarks: res.res.remarks,
                        del_flag: res.res.del_flag
                        // filename: res.res.filename
                    };
                    if (!res.error) {
                        aj.result = notifyInfo;
                    } else {
                        aj.success = false;
                        aj.msg = '获取失败';
                    }
                }
            }

            return _this2.json(aj);
        })();
    }

    //下载文件
    downloadFileAction() {
        var _this3 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const data = _this3.get();

            data.file_name = decodeURIComponent(data.file_name);
            data.file_path = decodeURIComponent(data.file_path);

            // const userAgent = this.userAgent.toLowerCase();
            // let hfilename = '';
            // if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
            //     hfilename = `=${encodeURIComponent(data.file_name)}`;
            // } else if(userAgent.indexOf('firefox') >= 0) {
            //     hfilename = `*="utf8''${encodeURIComponent(data.file_name)}"`;
            // } else {
            //     hfilename = `=${new Buffer(file_name).toString('binary')}`;
            // }
            // this.set('Content-Disposition', `attachment; filename${hfilename}`)
            return _this3.download(_path2.default.join(think.ROOT_PATH, 'www', 'static', 'upload', 'other', data.file_name));
        })();
    }
};